package cn.lili.controller.wxpay;

import com.jfinal.json.FastJson;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.Ret;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author User
 */
@SuppressWarnings({"SpellCheckingInspection", "AlibabaUndefineMagicConstant", "DuplicatedCode"})
@Controller
@RequestMapping("/buyer/wx/test/93709a6824c900f63046234fdbe5934b")
public class TestController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(TestController.class);


    @RequestMapping("")
    @ResponseBody
    public Ret index(HttpServletRequest request) {
        String notifyUrl = getBaseUrl(request) + "/buyer/wx/test/93709a6824c900f63046234fdbe5934b/doNotify";
        String amount = StringUtils.defaultIfEmpty(request.getParameter("amount"), "0.01");
        String storeId = StringUtils.defaultIfEmpty(request.getParameter("storeId"), "0.01");
        String type = StringUtils.defaultIfEmpty(request.getParameter("type"), "h5");
        Ret params = Ret.create()
                .set("notifyUrl", notifyUrl)
                .set("type", type)
                .set("orderId", System.currentTimeMillis())
                .set("storeId", storeId)
                .set("amount", amount)
                .set("preAmount", amount);

        String requestUrl = getBaseUrl(request) + "/buyer/wx/pre?p=" + base64Encode(encrypt(params.toJson()));
        log.info("微信支付请求地址：{}", requestUrl);

        return FastJson.getJson().parse(HttpKit.get(requestUrl), Ret.class);
    }

    @RequestMapping("/doNotify")
    @ResponseBody
    public Ret doNotify(HttpServletRequest request) {
        String orderId = request.getParameter("orderId");
        String createTime = request.getParameter("timeEnd");
        String transId = request.getParameter("transId");
        String amount = request.getParameter("amount");

        log.info("--测试订单回调成功：{} {} {} {}", orderId, amount, transId, createTime);

        return Ret.ok();
    }
}
